/*
 * @Author: Tendo
 * @Date: 2022-04-28 17:11:47
 * @LastEditTime: 2022-04-29 17:53:40
 * @LastEditors: Tendo
 * @Description: 
 * @FilePath: \TS\通用方法.ts
 */
let objData: object = {
    name: "zhangsan", age: 18,
    meal: true,
    lession: ["语文", "数学", "英语"],
    character: {
        option1: 1,
        option2: 2
    }
}



function deepClone<T>(data: T): T {
    function typeOf(obj: T): string {
        const toString: Function = Object.prototype.toString
        const map: any = {
            "[object Boolean]": "boolean",
            "[object Number]": "number",
            "[object String]": "string",
            "[object Function]": "function",
            "[object Array]": "array",
            "[object Date]": "date",
            "[object RegExp]": "regExp",
            "[object Undefined]": "undetined",
            "[object Null]": "null",
            "[object Object]": "object"
        }
        return map[toString.call(obj)]
    }

    // 获取传入拷贝函数的数据类型
    const type = typeOf(data)
    // 定义一个返回any类型的数据
    let reData: any;
    // 递归遍历一个array类型数据
    if (Array.isArray(data)) {
        reData = []
        for (let i = 0; i < data.length; i++) {
            reData.push(deepClone(data[i]))
        }
    } else if (type === "object") {//递归遍历一个object类型数据
        reData = {}
        for (const i in data) {
            reData[i] = deepClone(data[i])
        }
    }
    else {
        // 返回基本数据类型
        return data
    }
    // 将any类型的数据return出去，作为deepClone的结果
    return reData
}

let obj2: any = deepClone(objData)
console.log(obj2 === objData); // false


